package com.renwu.edu.info.manager.service;

import com.renwu.edu.info.manager.dao.TeacherDao;
import com.renwu.edu.info.manager.domain.Teacher;

public class TeacherService {
    private TeacherDao teacherDao = new TeacherDao();
    public boolean AddTeacher(Teacher tea) {
        return teacherDao.AddTeacher(tea);
    }

    public boolean isExists(int id) {
        Teacher[] tea = TeacherDao.findAllTeacher();
        boolean exists = false;
        for (int i = 0; i < tea.length; i++) {
            if(tea[i] != null && tea[i].getId() == id){
                exists = true;
                break;
            }
        }
        return exists;
    }

    public Teacher[] findAllTeacher() {
        Teacher[] teachers = teacherDao.findAllTeacher();
        //查看数组中有无信息，当数组中只要有一个元素不为null，则代表有信息[a,b,c,d,e]、[a.null,null,null,null];
        boolean result = false;
        for (int i = 0; i < teachers.length; i++) {
            //取出该数组中的每一个元素，
            //数组当中至少有一个元素信息，(只要有一个不为null)，就可以改变result值
            Teacher teacher = teachers[i];
            if(teacher != null){
                result = true;
                break;
            }
        }
        if(result){
            //返回对象地址
            return teacherDao.findAllTeacher();
        }else{
            //数组为空返回null:
            return null;
        }
    }

    public void deleteTeacher(int id) {
        teacherDao.deleteTeacher(id);
    }

    public void setTeacher(int id, Teacher teacher) {
        teacherDao.setTeacher(id,teacher);
    }
}
